package com.agrimall.servlet.merchant;

import com.agrimall.entity.User;
import com.agrimall.service.UserService;
import com.agrimall.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 商家个人中心Servlet
 */
@WebServlet("/merchant/user/profile")
public class MerchantProfileServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        User merchant = (User) session.getAttribute("merchant");
        
        // 验证商家登录状态
        if (merchant == null) {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        
        // 从数据库重新获取最新商家信息（包括余额等）
        User latestMerchant = userService.getUserById(merchant.getUserId());
        if (latestMerchant != null) {
            // 更新session中的商家信息
            session.setAttribute("merchant", latestMerchant);
            request.setAttribute("merchant", latestMerchant);
        } else {
            request.setAttribute("merchant", merchant);
        }
        
        request.getRequestDispatcher("/merchant/profile.jsp").forward(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        User sessionMerchant = (User) session.getAttribute("merchant");
        
        // 验证商家登录状态
        if (sessionMerchant == null) {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        
        // 获取表单参数
        String realName = request.getParameter("realName");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        
        // 创建更新对象
        User updateMerchant = new User();
        updateMerchant.setUserId(sessionMerchant.getUserId());
        updateMerchant.setRealName(realName);
        updateMerchant.setPhone(phone);
        updateMerchant.setEmail(email);
        
        // 更新商家信息
        boolean success = userService.updateUser(updateMerchant);
        
        if (success) {
            // 重新获取更新后的商家信息
            User updatedMerchant = userService.getUserById(sessionMerchant.getUserId());
            if (updatedMerchant != null) {
                // 更新session中的商家信息
                session.setAttribute("merchant", updatedMerchant);
                request.setAttribute("merchant", updatedMerchant);
                request.setAttribute("message", "个人信息修改成功！");
            } else {
                request.setAttribute("merchant", sessionMerchant);
                request.setAttribute("error", "更新成功，但获取最新信息失败");
            }
        } else {
            request.setAttribute("merchant", sessionMerchant);
            request.setAttribute("error", "个人信息修改失败，请重试");
        }
        
        request.getRequestDispatcher("/merchant/profile.jsp").forward(request, response);
    }
}



